<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="文档管理">
<meta name="author" content="文档管理">
<title>如何自定义 Tag 文档管理</title>
<link href="./assets/css/bootstrap.min.css" rel="stylesheet">
<!-- Documentation extras -->
<link href="./assets/css/docs.css" rel="stylesheet">
<link href="./assets/css/style.css" rel="stylesheet">
<link href="http://cdn.bootcss.com/highlight.js/7.3/styles/github.min.css" rel="stylesheet">
<style>
body{font-family:"ff-tisa-web-pro-1","ff-tisa-web-pro-2","Lucida Grande","Helvetica Neue",Helvetica,Arial,"Hiragino Sans GB","Hiragino Sans GB W3","WenQuanYi Micro Hei",sans-serif;}
h1, .h1, h2, .h2, h3, .h3, h4, .h4, .lead {font-family:"ff-tisa-web-pro-1","ff-tisa-web-pro-2","Lucida Grande","Helvetica Neue",Helvetica,Arial,"Hiragino Sans GB","Hiragino Sans GB W3","Microsoft YaHei UI","Microsoft YaHei","WenQuanYi Micro Hei",sans-serif;}
pre code { background: transparent; }
@media (min-width: 768px) {
    .bs-docs-home .bs-social, 
    .bs-docs-home .bs-masthead-links {
      margin-left: 0;
    }
}
.bs-docs-section p {
	line-height: 2;
}
.bs-docs-section p.lead {
	line-height: 1.4;
}
</style>
<!--[if lt IE 9]><script src="../docs-assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
  <script src="http://cdn.bootcss.com/html5shiv/3.7.0/html5shiv.min.js"></script>
  <script src="http://cdn.bootcss.com/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<!-- Favicons -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="../docs-assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="shortcut icon" href="../docs-assets/ico/favicon.png">
  </head>
  <body>
    <a class="sr-only" href="#content">Skip to main content</a>

    <!-- Docs master nav -->
    <header class="navbar navbar-inverse navbar-fixed-top bs-docs-nav" role="banner">
  <div class="container">
    <div class="navbar-header">
      <button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a href="./overview.html" class="navbar-brand">文档管理</a>
    </div>
    <nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation">
      <ul class="nav navbar-nav">
       		<li  ><a href="overview.html">简介</a></li>
            <li ><a href="download.html">下载</a></li>
            <li ><a href="userguide.html">开发指南</a></li>
            <li ><a href="config.html">配置指南</a></li>
            <li ><a href="syntax.html">语法指南</a></li>
            <li ><a href="integrate.html">Web 框架集成</a></li>
            <!--
            <li ><a href="example.html">例子</a></li>
            -->
            <li ><a href="faq.html" class="selected">FAQ</a></li>
            <li ><a href="about.html">关于</a></li>
      </ul>
    </nav>
  </div>
</header>
    <!-- Docs page layout -->
    <div class="bs-header" id="content">
      <div class="container">
        <h2>如何自定义 Tag</h2>
        <p></p>
      </div>
    </div>
	<a href="#top" id="toTop"></a>
    <!-- Callout for the old docs link -->
    <div class="container bs-docs-container">
      <div class="row">
        <div class="col-md-3">
          <div class="bs-sidebar hidden-print" role="complementary">
            <ul class="nav bs-sidenav">
            <li><a href="#x223019712">1 如何自定义 Tag</a></li>
            <li><a href="#x807110256">&nbsp&nbsp1.1 Cache Tag 代码范例：</a></li>
            <li><a href="#x718697860">&nbsp&nbsp1.2 Cache Tag 代码说明</a></li>
            </ul>
          </div>
        </div>
	        <div class="col-md-9" role="main">
	         <div class="bs-docs-section">
	           <h1 id="x223019712" class="page-header"><span>1 如何自定义 Tag</span></h1>
<p>jetbrick-template 支持自定义 Tag，类似于 JSP Taglib，但是要比 JSP Taglib 更简单更好用。</p>
<p>这里以建立一个支持 Cache 的 Tag 为例。</p>
<h2 id="x807110256" class="page-header"><span>1.1 Cache Tag 代码范例：</span></h2>
<h3 id="x2047045847" class="page-header"><span>1.1.1 首先定义一个 Tag 实现方法</span></h3>
<p>每一个自定义的 Tag 对应一个 Java 的 <code>public</code> <code>static</code> 的方法。比如我们这里定义的 cache tag，如下:</p>
<p>Tag 方法定义：</p> 
<div class="highlight">
 <pre><code class="language-html">public class MyTags {
    public static void cache(JetTagContext ctx, String name, int timeout) throws IOException {
        Cache cache = CacheManager.getCache(); // 请用自己的 Cache 代替
        Object value = cache.get(name);
        if (value == null) {
            value = ctx.getBodyContext();
            cache.put(name, value, timeout);
        }
        ctx.getWriter().print(value);
    }
}
</code></pre>
</div>
<h3 id="x1610375963" class="page-header"><span>1.1.2 必须要注册到全局的 JetEngine 中，我们使用如下的配置：</span></h3> 
<div class="highlight">
 <pre><code class="language-html">import.tags = MyTags, ...
</code></pre>
</div>
<h3 id="x289378703" class="page-header"><span>1.1.3 然后写对应的例子模板：</span></h3> 
<div class="highlight">
 <pre><code class="language-html">#tag cache(&quot;sum&quot;, 10)
    计算结果将被缓存10秒： ${1+2+3+4+5+6+7+8+9}
#end
</code></pre>
</div>
<h2 id="x718697860" class="page-header"><span>1.2 Cache Tag 代码说明</span></h2>
<p>对于每一个 Tag 的方法声明，有如下要求：</p> 
<ul> 
 <li>方法签名必须是 <code>public</code> <code>static</code></li> 
 <li>方法返回值必须是 <code>void</code></li> 
 <li>方法第一个参数必须是 <code>JetTagContext</code>， 其余参数自定义</li> 
 <li>允许 throws 任意的 Throwable</li> 
 <li>允许定义相同名字的 Tag，但是方法参数不一样 （Overload）</li> 
 <li>支持可变参数 (VarArgs)</li> 
</ul>
<p>在 Tag 方法中，我们可以通过 <code>JetTagContext</code> 来获取相关内容。主要 API 如下：</p> 
<ul> 
 <li>String JetTagContext.getBodyContext()<br /> 获取 <code>#tag ... #end</code> 之间的内容</li> 
 <li>void JetTagContext.writeBodyContext()<br /> 将内容原封不动的在原地进行输出</li> 
 <li>void JetTagContext.getWriter().print(...)<br /> 自定义输出内容</li> 
 <li>JetContext JetTagContext.getContext()<br /> 获取模板管理的 <code>JetContext</code> 对象，在 Web 环境中，可以通过 <code>JetContext</code> 对象进一步获取 <code>request</code>, <code>response</code> 等对象。</li> 
 <li>JetEngine JetTagContext.getEngine()<br /> 获取模板全局 Engine。</li> 
</ul>
<p>详细了解 <code>JetTagContext</code> 具体 API，请看 apidocs。</p>
<p>关于模板的使用：</p> 
<ul> 
 <li>Tag 的参数必须和定义的一致。<br /> 如果定义为: <code>cache(JetTagContext ctx, String name, int timeout)</code>，那么在调用的时候，必须传2个参数，一个 String， 一个 int，比如： <code>#tag cache(&quot;abc&quot;, 123) ... #end</code></li> 
</ul>
           	</div>
           </div>
		</div> 
 	</div>
    <!-- Footer
    ================================================== -->
    <footer class="bs-footer" role="contentinfo">
      <div class="container">
        <ul class="footer-links">
          <li>当前版本： v0.0.3</li>
          <li class="muted">&middot;</li>
          <li><a href="http://v2.bootcss.com/">Bootstrap 2.3.2 中文文档</a></li>
          <li class="muted">&middot;</li>
          <li><a href="http://www.bootcss.com">Bootstrap中文网</a></li>
          <li class="muted">&middot;</li>
          <li><a href="http://blog.getbootstrap.com">官方博客</a></li>
          <li class="muted">&middot;</li>
          <li><a href="https://github.com/twbs/bootstrap/issues?state=open">Issues</a></li>
          <li class="muted">&middot;</li>
          <li><a href="https://github.com/twbs/bootstrap/releases">Releases</a></li>
        </ul>
      </div>
    </footer>

    <!-- JS and analytics only. -->
<script src="http://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
<script src="./assets/js/bootstrap.js"></script>
<script src="http://cdn.bootcss.com/holder/2.0/holder.min.js"></script>
<script src="http://cdn.bootcss.com/highlight.js/7.3/highlight.min.js"></script>
<script >hljs.initHighlightingOnLoad();</script>
<script src="./assets/js/application.js"></script>
<script src="./assets/js/jquery.scrollToTop.min.js"></script>
<script text="javascript/text">
	$(document).ready(function(){
		$("#toTop").scrollToTop(1000);
  		$("table").addClass("table table-bordered");
  	});
</script>
  </body>
</html>
